import React, { Component } from 'react';
import { ImageBackground, FlatList, View, Image, RefreshControl, Text, TouchableOpacity, TextInput, StyleSheet } from 'react-native';
import { unitWidth, width } from '../constants/AdapterUtil';
import { MyHeader, MyRightImageHeader } from '../components/MyHeader';
import { api_member_info_bankList, api_member_info_delBank, api_member_info_followList } from '../constants/HttpServices';
import { postFetch } from '../constants/HttpExtension';
import Colors from '../constants/Colors';
import { show } from '../constants/Toast';
import MyListView from '../components/MyListView';
import { Rating, AirbnbRating } from 'react-native-elements';
import { SMButton, SDButton } from '../components/Button';
import CommonUtil from '../constants/CommonUtil';

export default class MyFollowScreen extends Component {

    constructor(props) {
        super(props)
        this.state = {
            list: [],
        }
    }

    componentDidMount() {
        this.props.navigation.addListener('didFocus', payload => {
            this.getData(1);
        })
    }

    render() {

        return (
            <View style={{ flex: 1, backgroundColor: Colors.colorF5 }}>
                <MyHeader title='我的关注' leftImg={require('../res/ic_arrow_left.png')} leftClick={() => { this.props.navigation.pop() }} />
                <MyListView
                    ref='MyListView'
                    list={this.state.list}
                    renderItem={this.getItem.bind(this)}
                    getList={this.getData.bind(this)}
                    canLoadMore={true}
                />
            </View>
        );
    }

    getItem({ item, index }) {
        return (
            <View style={{
                width: width,
                height: 145 * unitWidth,
                backgroundColor: Colors.white, justifyContent: 'center'
            }}>
                <TouchableOpacity style={{
                    paddingHorizontal: 20 * unitWidth,
                    flexDirection: 'row', alignItems: 'center'
                }} activeOpacity={1} onPress={() => {
                    this.props.navigation.navigate('ServiceProviderDetail', { merchid: item.id })
                }}>
                    <Image style={{ width: 115 * unitWidth, height: 90 * unitWidth }} source={{ uri: item.logo }} resizeMode='stretch' />
                    <View style={{ alignItems: 'flex-start', marginLeft: 7 * unitWidth }}>
                        <Text style={{ color: Colors.black, fontSize: 15 }}>{item.merchname}</Text>
                        <Rating
                            style={{ marginTop: 7 * unitWidth }}
                            readonly={true}
                            startingValue={item.start}
                            imageSize={20}
                        />
                        <View style={{ flexDirection: 'row', alignItems: 'center', marginTop: 7 * unitWidth }}>
                            <Image style={{ width: 13 * unitWidth, height: 16 * unitWidth }} source={require('../res/syss_dddw.png')} resizeMode='stretch' />
                            <Text style={{ color: Colors.color99, fontSize: 13, marginLeft: 5 * unitWidth }}>{item.area}</Text>
                        </View>
                        <View style={{ flexDirection: 'row', alignItems: 'center', marginTop: 7 * unitWidth }}>
                            {item.is_realname == '1' ? <SMButton id={item.id} /> : null}
                            {item.is_on_the_spot == '1' ? <SDButton style={{ marginLeft: 5 * unitWidth }} id={item.id} /> : null}
                        </View>
                    </View>
                    <TouchableOpacity style={{ alignSelf: 'flex-start', alignItems: 'center', position: 'absolute', right: 15 * unitWidth, }} activeOpacity={0.5}
                        onPress={() => {
                            CommonUtil.cancelfollow(item.id, {
                                success: (response) => {
                                    this.refs.MyListView.onRefresh();
                                },
                                fail: (response) => {

                                }
                            })
                        }}>
                        <Image style={{ width: 18 * unitWidth, height: 16 * unitWidth }} source={require('../res/syss_guanzhu.png')} resizeMode='stretch' />
                        <Text style={{ color: Colors.color66, fontSize: 13, marginTop: 10 * unitWidth }}>关注</Text>
                    </TouchableOpacity>
                </TouchableOpacity>
                <Text style={{ color: Colors.color66, fontSize: 14, marginTop: 10 * unitWidth, marginHorizontal: 20 * unitWidth }}>主营业务：{item.salecate}</Text>
                {index == this.state.list.length - 1 ?
                    null : <View style={{ backgroundColor: Colors.colorE8, width: 338 * unitWidth, alignSelf: 'center', height: 0.5, marginTop: 10 * unitWidth, position: 'absolute', bottom: 0 }} />
                }
            </View>
        )
    }

    //获取
    getData(page) {
        let params = {
            page: page
        }
        postFetch(api_member_info_followList, params, {
            success: (response) => {
                if (page == 1) {
                    this.refs.MyListView.onHasMore();
                    this.setState({
                        list: response.result.list,
                    })
                } else {
                    if (response.result.list && response.result.list.length > 0) {
                        this.refs.MyListView.onHasMore();
                        this.setState({
                            list: this.state.list.concat(response.result.list),
                        })
                    } else {
                        this.refs.MyListView.onNoMore();
                    }
                }
            },
            fail: (response) => {
                this.refs.MyListView.onNoMore();
                show(response.result.message);
            }
        })
    }
}